package com.littlebee.storysuper.adapter;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;


import com.littlebee.storysuper.R;
import com.littlebee.storysuper.activity.BrowserDetailActivity;
import com.littlebee.storysuper.entry.CircleEntry;
import com.littlebee.storysuper.widget.NoScrollGridView;

public class CircleAdapter extends BaseAdapter {

    private List<CircleEntry> mCircleBeans;
    private Context mContext;
    private LayoutInflater mLayoutInflater;

    public CircleAdapter(List<CircleEntry> beans, Context context) {
        this.mCircleBeans = beans;
        this.mContext = context;
        mLayoutInflater = LayoutInflater.from(context);

    }

    @Override
    public int getCount() {
        return mCircleBeans.size();
    }

    @Override
    public Object getItem(int position) {
        return mCircleBeans.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = mLayoutInflater.inflate(R.layout.item_listview_circle,parent, false);
            holder = new ViewHolder();
            holder.gridView = (NoScrollGridView) convertView.findViewById(R.id.gridView);
            holder.tv_nickname = (TextView) convertView.findViewById(R.id.tv_nickname);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        final CircleEntry bean = (CircleEntry) getItem(position);
        if (null != bean) {

            // 设置时间
            holder.tv_nickname.setText(bean.date);
            // 判断如果用户是否上传图片
            if (bean.list != null && bean.list.size() > 0) {
                // 有：设置Adapter显示图片
                holder.gridView.setVisibility(View.VISIBLE);
                // 图片数组转图片集合
                final List<String> urls1=new ArrayList<String>();
                for (int i=0;i<bean.list.size();i++){
                    urls1.add(bean.list.get(i).photominiurl);
                }

                final String[] urls = urls1.toArray(new String[urls1.size()]);
                holder.gridView.setAdapter(new CircleGridAdapter(urls, mContext));
                // 设置点击事件
                holder.gridView.setOnItemClickListener(new OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent,
                                            View view, int position, long id) {
                        enterPhotoDetailed((ArrayList<String>)urls1, position);
                    }
                });
            } else {
                // 否：隐藏
                holder.gridView.setVisibility(View.GONE);
            }
        }
        return convertView;
    }

    /**
     * 进入图片详情页
     *
     * @param position 角标
     */
    protected void enterPhotoDetailed(ArrayList<String> urls, int position) {
        Intent intent = new Intent(mContext, BrowserDetailActivity.class);
        intent.putStringArrayListExtra(BrowserDetailActivity.EXTRA_IMAGE_URLS, urls);
        intent.putExtra(BrowserDetailActivity.EXTRA_IMAGE_INDEX, position);
        mContext.startActivity(intent);
    }

    private static class ViewHolder {
        ImageView iv_icon;
        TextView tv_nickname;
        TextView tv_dynamicDesc;
        TextView tv_releaseTime;
        TextView tv_goodCount;
        TextView tv_commentCount;
        NoScrollGridView gridView;
    }
}